package com.wef.system.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.Hibernate;

import com.gfa4j.exception.ValidateRuntimeException;
import com.gfa4j.hibernate.BaseObject;
import com.gfa4j.service.impl.BaseServiceImpl;
import com.wef.system.entity.Role;
import com.wef.system.entity.RoleUser;
import com.wef.system.entity.User;
import com.wef.system.entity.domain.RoleDomain;
import com.wef.system.entity.domain.RoleUserDomain;
import com.wef.system.entity.domain.UserDomain;
import com.wef.system.service.RoleUserService;

public class RoleUserServiceImpl extends BaseServiceImpl implements RoleUserService {

	public RoleUser addRoleUser(RoleUser roleUser){
		
		return (RoleUser) super.saveObject(roleUser);
	}

	public void deleteRoleUser(RoleUser roleUser) {		
		super.deleteObject(roleUser);
		
	}

	public RoleUser saveOrUpdateRoleUser(RoleUser roleUser){	
		if (roleUser.getRowStatus() == BaseObject.ROWSTATUS_ADDED) {
			return this.addRoleUser(roleUser);
		}
		if (roleUser.getRowStatus() == BaseObject.ROWSTATUS_MODIFIED) {
			return this.updateRoleUser(roleUser);
		}
		if (roleUser.getRowStatus() == BaseObject.ROWSTATUS_DELETED) {
			this.deleteRoleUser(roleUser);
			return null ;
		}
		return roleUser;
	}

	public Set<RoleUser> saveOrUpdateRoleUsers(Set<RoleUser> roleUsers){
		StringBuilder errorString = new StringBuilder();
		for (RoleUser roleUser : roleUsers) {
			try {
				saveOrUpdateRoleUser(roleUser);
			} catch (ValidateRuntimeException ex) {
				errorString.append(ex.getMessage() + "\n");
			}
		}
		if (errorString.length() > 0) {
			throw new ValidateRuntimeException(errorString.toString());
		}
		return roleUsers;
	}

	public RoleUser updateRoleUser(RoleUser roleUser) {
		return (RoleUser) super.updateObject(roleUser);
	}	
	
	public List<RoleUser> loadUserRoleByUserId(String userId) {
		List<String> initProps = new ArrayList<String>();
		initProps.add(UserDomain.roleUsers);
		List lst = (List)super.findByProperty(User.class, UserDomain.userId, userId, initProps);
		
		if(lst != null && lst.size() > 0){
			User user = (User)lst.get(0);
			return new ArrayList<RoleUser>(user.getRoleUsers());
		}
		return new ArrayList<RoleUser>();
	}
	
	public List<RoleUser> getUserRoleByUserId(String userId) {
		List<RoleUser> roleUserList = new ArrayList<RoleUser>();
		
		List<User> lst = (List<User>)super.findByProperty(User.class, UserDomain.userId, userId);
		if(lst != null && lst.size() > 0){
			User user = (User)lst.get(0);
			Hibernate.initialize(user.getRoleUsers());
			Iterator<RoleUser> iter = user.getRoleUsers().iterator();
	
			while(iter.hasNext())
			{
				Hibernate.initialize(iter.next().getRole());
			}
			
			roleUserList = new ArrayList<RoleUser>(user.getRoleUsers());
			
		}
		
		return roleUserList;
	}

	public void saveRoleUsers(String userId, List<String> roleIds) {
		User user = (User)super.loadByPK(User.class, UserDomain.userId, userId);
		
		// delete old configures
		super.deleteObjectByProperty(RoleUser.class, RoleUserDomain.user, user);
		
		// save new configures
		Set<RoleUser> setRoleUser = new HashSet<RoleUser>();
		for(String roleId : roleIds){
			RoleUser roleUser = new RoleUser();
			roleUser.setUser(user);
			Role role = (Role)super.loadByPK(Role.class, RoleDomain.roleId, roleId);
			roleUser.setRole(role);
			roleUser.setRowStatus(BaseObject.ROWSTATUS_ADDED);
			setRoleUser.add(roleUser);
		}
		this.saveOrUpdateRoleUsers(setRoleUser);
	}
}
